package com.maimang.platform.crm.service.impl;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiDepartmentGetRequest;
import com.dingtalk.api.request.OapiDepartmentListIdsRequest;
import com.dingtalk.api.request.OapiDepartmentListParentDeptsByDeptRequest;
import com.dingtalk.api.request.OapiDepartmentListRequest;
import com.dingtalk.api.response.OapiDepartmentGetResponse;
import com.dingtalk.api.response.OapiDepartmentListIdsResponse;
import com.dingtalk.api.response.OapiDepartmentListParentDeptsByDeptResponse;
import com.dingtalk.api.response.OapiDepartmentListResponse;
import com.maimang.platform.crm.config.URLConstant;
import com.maimang.platform.crm.service.DingTalkDeptService;
import com.maimang.platform.crm.util.AccessTokenUtil;
import com.maimang.platform.lang.StringUtils;
import org.springframework.stereotype.Service;

/**
 * @author Rocky
 * @version 1.0.0
 * @classname DingTalkDeptServiceImpl
 * @description <br>钉钉部门管理服务层实现</br>
 * @date 2019-09-22 13:45
 * @email jintaozhao@qq.com
 */
@Service("dingTalkDeptService")
public class DingTalkDeptServiceImpl implements DingTalkDeptService {

	/**
	 * 获取部门列表
	 * @param id         父部门id（如果不传，默认部门为根部门，根部门ID为1）
	 * @param fetchChild 是否递归部门的全部子部门，ISV微应用固定传递false
	 * @return
	 * @throws Exception
	 */
	@Override
	public OapiDepartmentListResponse getDeptList(String id, Boolean fetchChild) throws Exception {
		String accessToken = AccessTokenUtil.getToken();
		DingTalkClient client = new DefaultDingTalkClient(URLConstant.DEPT_LIST);
		OapiDepartmentListRequest request = new OapiDepartmentListRequest();
		if (StringUtils.isNotEmpty(id))
			request.setId(id);
		request.setHttpMethod("GET");
		request.setFetchChild(fetchChild);
		OapiDepartmentListResponse response = client.execute(request, accessToken);
		return response;
	}

	/**
	 * 获取部门详情
	 *
	 * @param id 部门id
	 * @return 部门详情对象
	 * @throws Exception
	 */
	@Override
	public OapiDepartmentGetResponse getDeptDetail(String id) throws Exception {
		if (StringUtils.isEmpty(id))
			return null;
		DingTalkClient client = new DefaultDingTalkClient(URLConstant.DEPT_DETAIL_GET);
		OapiDepartmentGetRequest request = new OapiDepartmentGetRequest();
		String accessToken = AccessTokenUtil.getToken();
		request.setId(id);
		request.setHttpMethod("GET");
		OapiDepartmentGetResponse response = client.execute(request, accessToken);
		return response;
	}

	/**
	 * 获取子部门ID列表
	 *
	 * @param id 父部门id。根部门的话传1
	 * @return
	 * @throws Exception
	 */
	@Override
	public OapiDepartmentListIdsResponse getDeptIds(String id) throws Exception {
		DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/department/list_ids");
		OapiDepartmentListIdsRequest request = new OapiDepartmentListIdsRequest();
		request.setId(id);
		request.setHttpMethod("GET");
		String accessToken = AccessTokenUtil.getToken();
		OapiDepartmentListIdsResponse response = client.execute(request, accessToken);
		return response;
	}

	/**
	 * 查询部门的所有上级父部门路径
	 *
	 * @param id 部门id
	 * @return
	 * @throws Exception
	 */
	@Override
	public OapiDepartmentListParentDeptsByDeptResponse listParentDeptsByDept(String id) throws Exception {
		if (StringUtils.isEmpty(id))
			return null;
		DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/department/list_parent_depts_by_dept");
		OapiDepartmentListParentDeptsByDeptRequest request = new OapiDepartmentListParentDeptsByDeptRequest();
		String accessToken = AccessTokenUtil.getToken();
		request.setId(id);
		request.setHttpMethod("GET");
		OapiDepartmentListParentDeptsByDeptResponse response = client.execute(request, accessToken);
		return response;
	}
}
